﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.AppExtract
{
    /// <summary>
    /// 获取外部程序提取数据的记录
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("AppExtract", "GetRecordList", "1.0")]
    public class GetRecordListAction : BaseAction<GetRecordListParams, GetRecordListResult>
    {
        protected override ActionResult<GetRecordListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetRecordListParams opParams)
        {
            SER.AppExtractService service = new SER.AppExtractService();
            PagedList<VIW.VAppExtractRecord> serResult = service.GetRecordPagedList(opParams.AppId, opParams.BeginTime, opParams.EndTime, opParams.Page*25, 25);
            if(serResult.IsSuccess)
            {
                GetRecordListResult result = new GetRecordListResult();
                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Page = opParams.Page;
                result.Total = serResult.Total;
                result.Count = serResult.Data.Count;

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetRecordListResult.Item item = new GetRecordListResult.Item();

                    item.Id = serResult.Data[index].Id;
                    item.AppId = serResult.Data[index].AppId;
                    item.BatchId = serResult.Data[index].BatchId;
                    item.AppName = serResult.Data[index].AppName;
                    item.CreatedTime = serResult.Data[index].CreatedTime;
                    item.DataCount = serResult.Data[index].DataCount;
                    item.DataServiceType = serResult.Data[index].DataServiceType;
                    item.DataSource = serResult.Data[index].DataSource;
                    item.DataSourceType = serResult.Data[index].DataSourceType;
                    item.DataSourceText = DefaultValue.GetValue(serResult.Data[index].DataSourceText, string.Empty);
                    item.DataServiceText = serResult.Data[index].DataServiceText;
                    item.ExtractedCount = serResult.Data[index].ExtractedCount;

                    item.RejectedCount = serResult.Data[index].RejectedCount;
                    //item.AppName = DefaultValue.GetValue(serResult.Data[index].a)

                    result.Items.Add(item);
                }

                return new ActionResult<GetRecordListResult>(result);
            }
            else
            {
                return new ActionResult<GetRecordListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetRecordListParams
    {
        /// <summary>
        /// 外部程序id
        /// </summary>
        public Guid AppId { get; set; }

        /// <summary>
        /// 起始时间
        /// </summary>
        public DateTime BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        [Default(0)]
        public int Page { get; set; }
    }

    public class GetRecordListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            /// <summary>
            /// 对方系统提取数据时的批次号
            /// </summary>
            public Guid BatchId { get; set; }

            public Guid DataServiceType { get; set; }

            public int DataSourceType { get; set; }

            public Guid DataSource { get; set; }
            public string DataServiceText { get; set; }
            public string DataSourceText { get; set; }

            public Guid AppId { get; set; }

            public int DataCount { get; set; }


            public DateTime CreatedTime { get; set; }


            public int ExtractedCount { get; set; }

            public int RejectedCount { get; set; }

            public string AppName { get; set; }
        }
    }
}
